import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login'
import Register from '@/views/Register'
import Home from '@/views/Home'
Vue.use(VueRouter)
const routes=[
    {
        path:'/login',
        component:Login
    },
    {
        path:'/register',
        component:Register,
    },
    {
        path:'/',
        redirect:'/home' //当用户首页项目根路径，重定向/home
    },
    {
        path:'/home',
        component:Home,
        beforeEnter:async(to,from,next)=>{
            //从localstorage中获取到token信息
            let token=localStorage.getItem('token')
            if(token){
                const {code}=await Vue.prototype.$api.users.getUserInfo()
                if(code){
                    next()
                }
            }else{
                alert('您还没有登录，请先登录')
                //回到登录页面上去
                next('/login')
            }
        },
        children:[
            {
                path:'',
                component:()=>import('@/views/ShortNav.vue'),
                meta:{
                   isKeepAlive:false,
                }
            },
            {
                path:'studentsList',
                component:()=>import('@/views/student/StudentList'),
                meta:{
                    isKeepAlive:false,
                    firstTitle:'学生管理',
                    secondTitle:'学生列表'
                 }
            },
            {
                path:'studentUpdate/:id',
                component:()=>import('@/views/student/StudentUpdate'),
                meta:{
                    isKeepAlive:false,
                    firstTitle:'学生管理',
                    secondTitle:'修改学生'
                },
                props:true
            },
            {
                path:'studentsDetail',
                component:()=>import('@/views/student/StudentDetail'),
                meta:{
                    isKeepAlive:false,
                    firstTitle:'学生管理',
                    secondTitle:'查看学生'
                },
            },
            {
                path:'studentsAdd',
                component:()=>import('@/views/student/StudentAdd'),
                meta:{
                    isKeepAlive:true,
                    firstTitle:'学生管理',
                    secondTitle:'增加学生'
                 }
            },
            {
                path:'classesList',
                component:()=>import('@/views/classes/ClassesList'),
                meta:{
                    isKeepAlive:false,
                    firstTitle:'班级管理',
                    secondTitle:'班级列表'
                 }
            },
            {
                path:'classesAdd',
                component:()=>import('@/views/classes/ClassesAdd'),
                meta:{
                    isKeepAlive:false,
                    firstTitle:'班级管理',
                    secondTitle:'增加班级'
                }
            }
        ]
    }
]
const router=new VueRouter({
    routes,
    mode:'history',
})
/*
  编写路由全局前置守卫
  to:表示是要进入的目标路由对象
  from:表示是要离开的目标路由对象
  next:是一个控制路由去向的控制函数
  1)next():继续进入到目标路由对象
  2)next("/path路径")：转发到指定的路由上去
  3）next(false)：取消导航跳转
*/
// router.beforeEach(async(to,from,next)=>{
//     //排除登录和注册的路由，如果遇到这两个路由直接让进入
//     if(to.path.includes('login')||to.path.includes('register')){
//         next()
//     }else{
//         //从localstorage中获取到token信息
//         let token=localStorage.getItem('token')
//         if(token){
//             const {code}=await Vue.prototype.$api.users.getUserInfo()
//             if(code){
//                 next()
//             }
//         }else{
//             alert('您还没有登录，请先登录')
//             //回到登录页面上去
//             next('/login')
//         }
//     }   
// })
export default router